package com.koron.system.anno;

import cn.hutool.extra.spring.SpringUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.dto.OutStaffBean;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
public class VerifyStaffAspect {

    private List<VerifyStaffProcess> processList;

    @PostConstruct
    public void init(){
        Map<String, VerifyStaffProcess> map = SpringUtil.getBeansOfType(VerifyStaffProcess.class);
        this.processList = map.values().stream().collect(Collectors.toList());
    }


    @Pointcut(value = "@annotation(com.koron.system.anno.VerifyStaff)")
    public void pointCut() {
    }

    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        OutStaffBean outStaffBean = (OutStaffBean) args[0];
        for (VerifyStaffProcess verifyStaffProcess : processList) {
            if (verifyStaffProcess.supports()) {
                Response response = verifyStaffProcess.verify(outStaffBean);
                return response;
            }
        }
        return joinPoint.proceed();
    }

}
